﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageList.ascx.cs" Inherits="TMT.Admin.Page.PageList" %>
<%@ Register Src="~/Page/PageTemplates.ascx" TagName="Template" TagPrefix="uc" %>
<script type="text/javascript">

    $(document).ready(function () {
        $("#btnCreateNew").live("click", function () {
            $('#EditPage').html($('#newPageTemplate').html());
            $('#EditPage').dialog({ width: 650, title: 'New Page',
                buttons: [
                            {
                                text: "Cancel",
                                click: function () { $(this).dialog("close"); }
                            },
                            {
                                text: "Save",
                                click: function () {
                                    SavePage();
                                    $(this).dialog("close");
                                }
                            }
                         ],
                open: function () {

                }
            });
        });
    });

    function SavePage() {
        var page = GetPageInfo();
        var pageInfo = "{'pageInfo':'" + JSON.stringify(page) + "'}";

        $.ajax({
            type: "POST",
            url: '<%= ResolveUrl("~/Services/PageService.asmx/AddPage") %>',
            data: pageInfo,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                ShowMessage(data.d);
            },
            error: function (x, e) {
                if (x.status == 0) {
                    ShowMessage('You are offline!!\n Please Check Your Network.');
                } else if (x.status == 404) {
                    ShowMessage('Requested URL not found.');
                } else if (x.status == 500) {
                    ShowMessage('Internel Server Error.');
                } else if (e == 'parsererror') {
                    ShowMessage('Error.\nParsing JSON Request failed.');
                } else if (e == 'timeout') {
                    ShowMessage('Request Time out.');
                } else {
                    ShowMessage('Unknow Error.\n' + x.responseText);
                }
            }
        });
    }

    function DeletePage(pageId) {
        $.ajax({
            type: "POST",
            url: '<%= ResolveUrl("~/Services/PageService.asmx/DeletePage") %>',
            data: "{'pageId': '" + pageId + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                ShowMessage(data.d);
            },
            error: function (x, e) {
                if (x.status == 0) {
                    ShowMessage('You are offline!!\n Please Check Your Network.');
                } else if (x.status == 404) {
                    ShowMessage('Requested URL not found.');
                } else if (x.status == 500) {
                    ShowMessage('Internel Server Error.');
                } else if (e == 'parsererror') {
                    ShowMessage('Error.\nParsing JSON Request failed.');
                } else if (e == 'timeout') {
                    ShowMessage('Request Time out.');
                } else {
                    ShowMessage('Unknow Error.\n' + x.responseText);
                }
            }
        });
    }

</script>
<div id="EditPage">
</div>
<input class="button" type="button" id="btnCreateNew" value="Create new page" />
<asp:Repeater ID="rptPages" runat="server">
    <HeaderTemplate>
        <table cellspacing="0">
            <tr>
                <th>
                    Page Title
                </th>
                <th>
                    Description
                </th>
                <th>
                    Url
                </th>
                <th>
                    Show on menu
                </th>
                <th>
                    Parent
                </th>
                <th>
                    Action
                </th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "BaseInfo.PageTitle")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "BaseInfo.Description")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "BaseInfo.Url")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "ShowOnMenuStatus")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "ParentPage")%>
            </td>
            <td>
                <img src="../icons/edit.png" class="action-button edit" alt="Edit" title="Edit" userid='<%#DataBinder.Eval(Container.DataItem, "BaseInfo.PageId")%>' />
                <img src="../icons/trash.png" class="action-button" alt="Remove" title="Remove" userid='<%#DataBinder.Eval(Container.DataItem, "BaseInfo.PageId")%>' />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
<uc:Template ID="PageTemplate" runat="server" />
